<?php
header('Content-Type:text/html;charset=utf-8');
date_default_timezone_set("Asia/Shanghai");
if (!(empty($_POST)))//不为空
{
    $username=isset($_POST["username"]) ? trim($_POST["username"]) : "";
    $password=isset($_POST["password"]) ? trim($_POST["password"]) : "";
    $re=  isset($_POST["re"]) ? trim($_POST["re"]) : false;
    $time=date("y-m-d h:i:sa" , time());
    //判断数据库有没有
    require 'db_conn.php';
    $sql="select * from t_user where username='$username'";
    $rs=$db->query($sql);
    if($rs) //有这个名字的用户
    {
        $row=$rs->fetch_assoc();
        if($row["password"]==$password)//密码正确
        {
            //把登陆成功的用户信息存到session
            session_start();
            $_SESSION["userinfo"]==array(
                "id"=>$row["id"]
            )
            header("Location:main.php")
            die;
        }
    }
    
         echo "<script language='javascript' type='text/javascript'>";
        echo "alert('登录失败,用户名和密码不正确！');";
        echo "window.location.href='index.php'";
        echo "</script>";
}
 $time=isset($_POST["time"]) ? trim($_POST["time"]) : "";
 setcookie("time",$time,  time()+3600*24);